<template >
    <div class="changeEamil">
        <div>
            <span> &nbsp;&nbsp;新邮箱:&nbsp;</span>
            <el-input v-model="form.email" placeholder="请输入新邮箱"></el-input>
        </div>
        <div>
            <span> 邮箱验证码:&nbsp;</span>
            <el-input v-model="form.code" placeholder="请输入邮箱验证码"></el-input>
        </div>

        <div class="btn">
            <el-button type="success" @click="sendCode">发送验证码</el-button>
            <el-button type="primary" @click="submit">确认</el-button>
        </div>

    </div>
</template>
<script>
import { getCode,updateEm } from "../../../utils/user.js";
    export default {
        data() {
                return {
                    params: {
                        userId: "",
                        token: "",
                    },
                    form: {
                        userId: '',
                        email: '',
                        code:''
                    }
                }
            },
            created() {
                this.form.userId = localStorage.getItem("user");
                this.params.token = this.$cookies.get("token");
            },
            methods: {
                submit(){
                    let rex = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
                    if(rex.test(this.form.email)){
                        updateEm(this.form,this.params).then(res=>{
                            if(res.data.code==200){
                                this.$message.success(res.data.msg);
                            }else{
                                this.$message.error(res.data.msg);
                            }
                        })
                    }else{
                        this.$message.error("新邮箱格式错误，请重新输入！")
                    }
                },
                sendCode(){
                    let rex = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
                    if(rex.test(this.form.email)){
                        getCode(this.form).then(res=>{
                            if(res.data.code==200){
                                this.$message.success(res.data.msg);
                            }else{
                                this.$message.error(res.data.msg);
                            }
                        })
                    }else{
                        this.$message.warning("新邮箱格式错误，请重新输入！");
                    }
                }
            },
    }
</script>
<style lang="less" scoped>
    @import url('./index.less');
</style>